package de.ciupka.minesweeper.options;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import de.ciupka.minesweeper.controller.Controller;

public class OptionPanel extends JPanel {
	
	private Difficulty difficulty;
	private Options options;
	private JButton cancel;
	private JButton apply;
	private Controller controller;
	
	public OptionPanel() {
		controller = Controller.getInstance();
		setSize(OptionFrame.FRAME_WIDTH, OptionFrame.FRAME_HEIGHT);
		setVisible(true);
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		difficulty = new Difficulty();
		options = new Options();
		addComponents();
		createActionListener();
	}
	
	private void addComponents() {
		addButtons();
		ArrayList<JComponent> components = difficulty.getComponents();
		components.addAll(options.getComponents());
		for(JComponent currentComponent : components) {
			add(currentComponent);
		}
		add(apply);
		add(cancel);
	}
	
	private void addButtons() {
		apply = new JButton("OK");
		apply.setBackground(Color.white);
		apply.setBounds(165, 345, 100, 20);
		cancel = new JButton("Abbrechen");
		cancel.setBackground(Color.white);
		cancel.setBounds(285, 345, 100, 20);
	}
	
	private void createActionListener() {
		apply.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				controller.getGamecreator().createNewGame();
				controller.getGUIController().closeFrame(controller.getGUIController().getOptionFrame());
			}
		});
		cancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				controller.getGUIController().closeFrame(controller.getGUIController().getOptionFrame());
			}
		});
	}
}
